//
//  OrderViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/21.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderTableViewCell.h"
#import "PendingTableViewCell.h"
#import "PendingInfoViewController.h"
#import "OrderInfoViewController.h"
#import "PendingListModel.h"
#import "positionsListModel.h"

@interface OrderViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)UIView * topView;

@property (nonatomic, copy)NSString * changeTableViewString;

@property (nonatomic, strong)UIButton * totalRankButton;

@property (nonatomic, strong)UIButton * monthRankButton;

@property (nonatomic, strong)UIView * leftLineView;

@property (nonatomic, strong)UIView * rightLineView;

@property (nonatomic, strong)NSArray<PendingListModel *> * pendingListArray;

@property (nonatomic, strong)NSArray<positionsListModel *> * positionsListArray;

@property (nonatomic, strong)NSTimer * countDownTimer;

@end

@implementation OrderViewController

//倒计时总的秒数
static NSInteger  secondsCountDown = 2;

- (void)viewWillAppear:(BOOL)animated{
    
    [PendingListModel pendingListWithSucess:^(NSArray<PendingListModel *> *array) {
        
        self.pendingListArray = array;
        
        //        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } error:^{
        NSLog(@"historyList error");
    }];
    
    //positionsListWithSucess
    [positionsListModel positionsListWithSucess:^(NSArray<positionsListModel *> *array) {
        
        self.positionsListArray = array;
        
        secondsCountDown = 2;
        
        [self setupDownTimer];
        
    } error:^{
        
        NSLog(@"positionsList error");
    }];
 
    
}

- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}


//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        [positionsListModel positionsListWithSucess:^(NSArray<positionsListModel *> *array) {
            
            self.positionsListArray = array;
            
        } error:^{
            
            NSLog(@"positionsList error");
        }];
        
        secondsCountDown = 2;
        
        [self setupDownTimer];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}

- (void)setPendingListArray:(NSArray<PendingListModel *> *)pendingListArray{
    
    _pendingListArray = pendingListArray;
    
    [self.tableView reloadData];
}


- (void)setPositionsListArray:(NSArray<positionsListModel *> *)positionsListArray{
    
    _positionsListArray = positionsListArray;
    
    [self.tableView reloadData];
}


- (void)setupNavBar{
    
    self.title = @"订单";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.changeTableViewString = @"OrderTableView";
    
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    [self setupChooseTopView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 119 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 64 * SCREENH6)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //        self.tableView.bounces = NO;
    
    [self.tableView registerClass:[OrderTableViewCell class] forCellReuseIdentifier:@"OrderCell"];
    
    [self.tableView registerClass:[PendingTableViewCell class] forCellReuseIdentifier:@"PendingCell"];
    
//    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"OrderHeaderView"];
    
    self.tableView.sectionHeaderHeight = 84 * SCREENH6;
    
    [self.view addSubview:self.tableView];
    
    
}


- (void)setupChooseTopView{
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64 * SCREENH6, SCREEN_WIDTH, 55 * SCREENH6)];
    
    self.topView.userInteractionEnabled = YES;
    self.topView.backgroundColor = ColorWithRGB(0x061a23);
    
    [self.view addSubview:self.topView];
    
    self.totalRankButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 185 * SCREENH6, 55 * SCREENH6)];
    
    self.totalRankButton.selected = YES;
    self.totalRankButton.backgroundColor = [UIColor clearColor];
    [self.totalRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
    [self.totalRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
    self.totalRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    [self.totalRankButton addTarget:self action:@selector(didTotalRankButton) forControlEvents:UIControlEventTouchUpInside];
    [self.totalRankButton setTitle:@"持仓单" forState:UIControlStateNormal];
    
    [self.topView addSubview:self.totalRankButton];
    
    //    [self.totalRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.leading.top.bottom.mas_equalTo(header);
    //        make.width.mas_equalTo(SCREEN_WIDTH / 2);
    //    }];
    
    self.leftLineView = [[UIView alloc] init];
    self.leftLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.leftLineView.alpha = 1;
    
    [self.topView addSubview:self.leftLineView];
    
    [self.leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.totalRankButton);
        make.bottom.mas_equalTo(self.topView).offset(-1 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    
    self.monthRankButton = [UIButton new];
    
    self.monthRankButton.selected = NO;
    self.monthRankButton.backgroundColor = [UIColor clearColor];
    [self.monthRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
    [self.monthRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
    self.monthRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    [self.monthRankButton addTarget:self action:@selector(didMonthRankButton) forControlEvents:UIControlEventTouchUpInside];
    [self.monthRankButton setTitle:@"挂单" forState:UIControlStateNormal];
    
    [self.topView addSubview:self.monthRankButton];
    
    [self.monthRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.bottom.mas_equalTo(self.topView);
        make.width.mas_equalTo(SCREEN_WIDTH / 2);
    }];
    
    self.rightLineView = [UIView new];
    
    self.rightLineView.backgroundColor = [UIColor whiteColor];
    self.rightLineView.alpha = 0.1;
    
    [self.topView addSubview:self.rightLineView];
    
    [self.rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.monthRankButton);
        make.bottom.mas_equalTo(self.topView).offset(-1 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.changeTableViewString isEqualToString:@"OrderTableView"]) {
        
        return self.positionsListArray.count;
//        return 10;
    }else{
        
        return self.pendingListArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.changeTableViewString isEqualToString:@"OrderTableView"]) {
        
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell" forIndexPath:indexPath];
        
        cell.model = self.positionsListArray[indexPath.row];
        
        return cell;
    }else{
        
        PendingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PendingCell" forIndexPath:indexPath];
        
        cell.model = self.pendingListArray[indexPath.row];
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.changeTableViewString isEqualToString:@"OrderTableView"]) {
        
        return 65 * SCREENH6;
        
    }else{
        
        return 95 * SCREENH6;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.hidesBottomBarWhenPushed = YES;
    
    
    if ([self.changeTableViewString isEqualToString:@"OrderTableView"]) {
        
        OrderTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        
        OrderInfoViewController * orderVC = [OrderInfoViewController new];
        
        orderVC.positionId = cell.positionId.text;
        
        [self.navigationController pushViewController:orderVC animated:YES];
        
    }else{
        
        PendingTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        
        PendingInfoViewController * pendingVC = [PendingInfoViewController new];
        
        pendingVC.orderId = cell.orderId.text;//@"2228138";
        
        [self.navigationController pushViewController:pendingVC animated:YES];
    }
    
    
    

}


//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    
//    
//    return 55 * SCREENH6;
//}


//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    
//    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderHeaderView"];
//    
//    header.contentView.backgroundColor = ColorWithRGB(0x061a23);
//    
//    self.totalRankButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 185 * SCREENH6, 55 * SCREENH6)];
//    
//    self.totalRankButton.selected = YES;
//    self.totalRankButton.backgroundColor = [UIColor clearColor];
//    [self.totalRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
//    [self.totalRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
//    self.totalRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
//    [self.totalRankButton addTarget:self action:@selector(didTotalRankButton) forControlEvents:UIControlEventTouchUpInside];
//    [self.totalRankButton setTitle:@"持仓单" forState:UIControlStateNormal];
//    
//    [header.contentView addSubview:self.totalRankButton];
//    
//    //    [self.totalRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
//    //        make.leading.top.bottom.mas_equalTo(header);
//    //        make.width.mas_equalTo(SCREEN_WIDTH / 2);
//    //    }];
//    
//    self.leftLineView = [[UIView alloc] init];
//    self.leftLineView.backgroundColor = ColorWithRGB(0x3dba8a);
//    self.leftLineView.alpha = 1;
//    
//    [header.contentView addSubview:self.leftLineView];
//    
//    [self.leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self.totalRankButton);
//        make.bottom.mas_equalTo(header.contentView).offset(-1 * SCREENH6);
//        make.height.mas_equalTo(1 * SCREENH6);
//    }];
//    
//    
//    self.monthRankButton = [UIButton new];
//    
//    self.monthRankButton.selected = NO;
//    self.monthRankButton.backgroundColor = [UIColor clearColor];
//    [self.monthRankButton setTitleColor:ColorWithRGB(0x3dba8a) forState:UIControlStateSelected];
//    [self.monthRankButton setTitleColor:ColorWithRGB(0xc8c8c8) forState:UIControlStateNormal];
//    self.monthRankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
//    [self.monthRankButton addTarget:self action:@selector(didMonthRankButton) forControlEvents:UIControlEventTouchUpInside];
//    [self.monthRankButton setTitle:@"挂单" forState:UIControlStateNormal];
//    
//    [header addSubview:self.monthRankButton];
//    
//    [self.monthRankButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.top.bottom.mas_equalTo(header);
//        make.width.mas_equalTo(SCREEN_WIDTH / 2);
//    }];
//    
//    self.rightLineView = [UIView new];
//    
//    self.rightLineView.backgroundColor = [UIColor whiteColor];
//    self.rightLineView.alpha = 0.1;
//    
//    [header addSubview:self.rightLineView];
//    
//    [self.rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self.monthRankButton);
//        make.bottom.mas_equalTo(header).offset(-1 * SCREENH6);
//        make.height.mas_equalTo(1 * SCREENH6);
//    }];
//    
//    return header;
//}


- (void)didTotalRankButton{
    
    self.monthRankButton.selected = NO;
    self.totalRankButton.selected = YES;
    self.leftLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.leftLineView.alpha = 1;
    self.rightLineView.backgroundColor = [UIColor whiteColor];
    self.rightLineView.alpha = 0.1;
    
    self.changeTableViewString = @"OrderTableView";
    
//    [self.tableView layoutIfNeeded];
    [self.tableView reloadData];
}


- (void)didMonthRankButton{
    
    self.monthRankButton.selected = YES;
    self.totalRankButton.selected = NO;
    self.leftLineView.backgroundColor = [UIColor whiteColor];
    self.leftLineView.alpha = 0.1;
    self.rightLineView.backgroundColor = ColorWithRGB(0x3dba8a);
    self.rightLineView.alpha = 1;
    
    self.changeTableViewString = @"PendingTableView";
    
//    [self.tableView layoutIfNeeded];
    [self.tableView reloadData];
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidDisappear:(BOOL)animated{
    
    [_countDownTimer invalidate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
